//
//  LSContactSelectTableViewCell.m
//  LaSaCivilAffairsDepartment
//
//  Created by 聂志康 on 2019/8/22.
//  Copyright © 2019 wpkj. All rights reserved.
//

#import "LSContactSelectTableViewCell.h"
#import "LSContactSelectTableViewModel.h"

@interface LSContactSelectTableViewCell ()
@property (nonatomic, strong) UIButton *selectButton;
@property (nonatomic, strong) UILabel *mainTitleLabel;
@property (nonatomic, strong) UILabel *subTitleLabel;
@property (nonatomic, strong) UILabel *departmentLabel;
@end

@implementation LSContactSelectTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
//        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [self.contentView addSubview:self.selectButton];
        [self.contentView addSubview:self.mainTitleLabel];
        [self.contentView addSubview:self.subTitleLabel];
        [self.contentView addSubview:self.departmentLabel];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    WS
    [self.selectButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView.mas_left).with.offset(10);
        make.centerY.equalTo(weakSelf.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    [self.mainTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.selectButton.mas_right).with.offset(5);
        make.top.equalTo(weakSelf.contentView.mas_top).with.offset(12);
        make.right.equalTo(weakSelf.contentView.mas_right).with.offset(50);
    }];
    [self.subTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.selectButton.mas_right).with.offset(5);
        make.bottom.equalTo(weakSelf.contentView.mas_bottom).with.offset(-12);
        make.right.equalTo(weakSelf.contentView.mas_right).with.offset(50);
    }];
    [self.departmentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.contentView.mas_right).with.offset(-15);
        make.centerY.equalTo(weakSelf.contentView.mas_centerY);
    }];
}

-(void)setModel:(LSContactSelectTableViewModel *)model {
    _model = model;
    self.selectButton.selected = model.isSelected;
    self.mainTitleLabel.text = model.realname;
    self.subTitleLabel.text = model.mobilephone;
    self.departmentLabel.text = model.departname;
}

- (UIButton *)selectButton {
    if (!_selectButton) {
        _selectButton = [[UIButton alloc] init];
        [_selectButton setImage:[UIImage imageNamed:@"yuanquan_selected"] forState:UIControlStateSelected];
        [_selectButton setImage:[UIImage imageNamed:@"yuanquan"] forState:UIControlStateNormal];
        [_selectButton addTarget:self action:@selector(selectButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectButton;
}

- (void)selectButtonClick:(UIButton *)button {
    button.selected = !button.isSelected;
    _model.isSelected = button.isSelected;
    if ([self.delegate respondsToSelector:@selector(selectButtonDidClick:)]) {
        [self.delegate selectButtonDidClick:_model];
    }
}

- (UILabel *)mainTitleLabel {
    if (!_mainTitleLabel) {
        _mainTitleLabel = [[UILabel alloc] init];
        _mainTitleLabel.textColor = hexColor(333333);
        _mainTitleLabel.font = [UIFont systemFontOfSize:15];
    }
    return _mainTitleLabel;
}

- (UILabel *)subTitleLabel {
    if (!_subTitleLabel) {
        _subTitleLabel = [[UILabel alloc] init];
        _subTitleLabel.textColor = hexColor(666666);
        _subTitleLabel.font = [UIFont systemFontOfSize:13];
    }
    return _subTitleLabel;
}

- (UILabel *)departmentLabel {
    if (!_departmentLabel) {
        _departmentLabel = [[UILabel alloc] init];
        _departmentLabel.textColor = hexColor(666666);
        _departmentLabel.font = [UIFont systemFontOfSize:13];
    }
    return _departmentLabel;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
